import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;


public class Ex3_1 {


	ArrayList<String> dnas;
	String[] bases = {"A","T","G","C"};
	int k;
	int d;
	ArrayList<String> muts = new ArrayList<String>();

	public Ex3_1 () {
		dnas = getDNA("C:\\Users\\josemfer\\Downloads\\motif_enumeration_data.txt");	
		k = 3;
		d = 1;		
	}

	/*
	public ArrayList<ArrayList<Integer>> getMutations(String seq, int d) {		
		int k =seq.length();		
		ArrayList<ArrayList<Integer>> combs = 
				new ArrayList<ArrayList<Integer>>();	
		for (int i=1;i<=d;i++) {			
			if (combs.size() == 0) {
				for (int h=0;h<k;h++) {
					ArrayList<Integer> pos = new ArrayList<Integer>();
					pos.add(h);
					combs.add(pos);
				}
			} else {
				ArrayList<ArrayList<Integer>> newCombs 
				= new ArrayList<ArrayList<Integer>>();
				for(int j= (i-2)*k;j<combs.size();j++) {
					ArrayList<Integer> comb = combs.get(j);
					Integer last = comb.get(comb.size()-1);
					for (int n=last+1;n<k;n++) {
						ArrayList<Integer> newComb = new ArrayList<Integer>(comb);
						newComb.add(n);
						newCombs.add(newComb);
					}													
				}

				if (newCombs.size() > 0) {
					combs.addAll(newCombs);
				}
			}			
		}// iterating d	
		
		return combs;
	} */


	private ArrayList<String> mutate(String seq, ArrayList<Integer> comb) {
		
		ArrayList<String> muts = new ArrayList<String>();
		muts.add(seq);
		for (Integer pos : comb) {
			ArrayList<String> newMuts = new ArrayList<String>();
			for (String mut : muts) {
				for (String base : bases) {
					newMuts.add(mut.substring(0, pos) + base + mut.substring(pos+1));
				}
			}
			muts = new ArrayList<String>(newMuts);			
		}
		
		return muts;	
	}


	public ArrayList<String> getMutations(String seq, int d) {		
		int k =seq.length();	
		ArrayList<String> mutations = new ArrayList<String>();
		ArrayList<ArrayList<Integer>> combs = 
				new ArrayList<ArrayList<Integer>>();	
		for (int i=1;i<=d;i++) {			
			if (combs.size() == 0) {
				for (int h=0;h<k;h++) {
					ArrayList<Integer> pos = new ArrayList<Integer>();
					pos.add(h);
					combs.add(pos);
					mutations.addAll(mutate(seq,pos));
				}
			} else {
				ArrayList<ArrayList<Integer>> newCombs 
				= new ArrayList<ArrayList<Integer>>();
				for(int j= 0;j<combs.size();j++) {
					ArrayList<Integer> comb = combs.get(j);
					Integer last = comb.get(comb.size()-1);
					for (int n=last+1;n<k;n++) {
						ArrayList<Integer> newComb = new ArrayList<Integer>(comb);
						newComb.add(n);
						newCombs.add(newComb);
						mutations.addAll(mutate(seq,newComb));
					}													
				}

				if (newCombs.size() > 0) {
					combs = (ArrayList<ArrayList<Integer>>) newCombs.clone();
				}
			}			
		}// iterating d	
		
		return mutations;
	}

	
	
	
	
	

	public static void main(String[] args) {
		Ex3_1 ex = new Ex3_1();       
		ArrayList<String> mutations = ex.getMutations("AAAA", 2);
		System.out.println(mutations);
	}	


	private ArrayList<String> getDNA(String file) {		
		ArrayList<String> lineas = new ArrayList<String>();		
		try {
			BufferedReader br = new BufferedReader(new FileReader(file));
			String line;
			while ((line = br.readLine()) != null) {
				lineas.add(line);
			}
			br.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
		return lineas;		
	}



}
